package com.zjsystem.zjsy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjsystem.zjsy.constant.Constant;
import com.zjsystem.zjsy.dto.ZjxdAssignment;
import com.zjsystem.zjsy.enums.AuditEnums;
import com.zjsystem.zjsy.mapper.ZjxdAssignmentMapper;
import com.zjsystem.zjsy.request.AssignmentRequest;
import com.zjsystem.zjsy.result.Result;
import com.zjsystem.zjsy.service.ZjxdAssignmentService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 作战任务主表 前端控制器
 * </p>
 *
 * @author wyf
 * @since 2024-07-11
 */
@Slf4j
@RestController
@RequestMapping("/zjsy/zjxdAssignment")
public class ZjxdAssignmentController {
    @Resource
    private ZjxdAssignmentService assignmentService;
    @Resource
    private ZjxdAssignmentMapper assignmentMapper;

    @ApiModelProperty("任务列表 新建任务")
    @PostMapping("/saveOrUpdateAssignment")
    public Result saveOrUpdateAssignment(@RequestBody AssignmentRequest request) {
        String id = request.getId();
        if (StringUtils.isBlank(id)) {
            try {
                assignmentService.saveAssignment(request);
                return Result.ok("任务新增成功");
            } catch (Exception e) {
                log.error("新增任务异常：{}", e.getMessage());
                return Result.fail().message("新增任务异常");
            }
        }
        try {
            assignmentService.updateAssignment(request);
            return Result.ok("任务修改成功");
        } catch (Exception e) {
            log.error("任务修改异常：{}", e.getMessage());
            return Result.fail().message("任务修改异常" + e.getMessage());
        }
    }

    @ApiModelProperty("任务列表 删除任务")
    @GetMapping("/deleteAssignment")
    public Result deleteAssignment(@RequestParam("id") String id) {
        ZjxdAssignment assignment = assignmentService.getById(id);
        if (assignment.getAssignStatus().equals(AuditEnums.CJZ.name())) {
            assignmentMapper.deleteAssignment(id, Constant.one);
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("本阶段不允许删除!");
    }

    @ApiModelProperty("任务列表 分页+模糊")
    @PostMapping("/getAssignment")
    public Result getAssignment(@RequestBody AssignmentRequest request) {
        try {
            IPage<ZjxdAssignment> page = assignmentService.getAssignment(request);
            return Result.ok(page);
        } catch (Exception e) {
            log.error("列表展示异常:{}", e.getMessage());
            return Result.fail().message("列表展示异常");
        }
    }

    @ApiModelProperty("任务列表 编辑回显")
    @GetMapping("/updateShow")
    public Result updateShow(@RequestParam("id") String id) {
        ZjxdAssignment assignmentServiceById = assignmentService.getById(id);
        return Result.ok(assignmentServiceById);
    }

    @ApiModelProperty("任务列表 下拉框")
    @GetMapping("/getAll")
    public Result getALl() {
        return Result.ok(assignmentService.getAll());
    }

    @ApiModelProperty("阶段显示")
    @GetMapping("/getStageNum")
    public Result getStageNum() {
        return Result.ok(assignmentService.getStageNum());
    }
}

